﻿using System;
using System.Data.Linq;
using System.Linq;
using BluePortal.Framework;

namespace BluePortal.Medias.Core
{
    public class BCPlaylist: AbstractBusinessComponent
    {
        #region Internal Methods methods

        private void InternalCreate(Playlist playlist)
        {
            if (playlist.PLID == Guid.Empty) playlist.PLID = Guid.NewGuid();
            Context.Playlists.InsertOnSubmit(playlist);
            Context.SubmitChanges();
        }

        private void InternalDelete(Guid plid)
        {
            var selectedPlaylist = Context.Playlists.SingleOrDefault(pl => pl.PLID == plid);

            if (selectedPlaylist != null)
            {
                Context.Playlists.DeleteOnSubmit(selectedPlaylist);
                Context.SubmitChanges();
            }
        }

        #endregion

        #region Internal Queries

        public IQueryable<Playlist> QueryFindAll()
        {
            var query = from pl in Context.Playlists
                        select pl;
            return query;
        }

        public IQueryable<Playlist> QueryFindByUser(string loginName)
        {
            var query = from pl in Context.Playlists
                        where pl.LoginName == loginName
                        orderby pl.PlayListName
                        select pl;

            return query;
        }

        public IQueryable<Playlist> QueryFindByUserWithMedia(string loginName)
        {
            DataLoadOptions ds = new DataLoadOptions();
            ds.LoadWith<Playlist>(pl => pl.MediaPlaylists);
            ds.LoadWith<MediaPlaylist>(vpl => vpl.Media);
            Context.LoadOptions = ds;

            var query = from pl in Context.Playlists
                        where pl.LoginName == loginName
                        select pl;

            return query;
        }

        #endregion

        #region IVideoPlayListService Members

        public void Delete(Guid plid)
        {
            InternalDelete(plid);
        }

        public void Add(Playlist playlist, Media media)
        {
            AssertNotNull(playlist, "playlist");
            AssertNotNull(media, "media");
            Add(playlist, null, media.ID);
        }

        public void Add(Playlist playlist, MediaPlaylist VideoPlaylist, Media media)
        {
            AssertNotNull(playlist, "playlist");
            AssertNotNull(VideoPlaylist, "VideoPlaylist");
            AssertNotNull(media, "media");
            Add(playlist, VideoPlaylist, media.ID);
        }

        public void Add(Playlist playlist, Guid ID)
        {
            AssertNotNull(playlist, "playlist");
            Add(playlist, null, ID);
        }

        public void Add(Playlist playlist, MediaPlaylist mediaPlaylist, Guid ID)
        {
            if ((playlist.PLID == Guid.Empty) || (Context.Playlists.SingleOrDefault(pl => pl.PlayListName == playlist.PlayListName) == null))
            {
                InternalCreate(playlist);
            }

            if (mediaPlaylist == null)
            {
                mediaPlaylist = new MediaPlaylist() { PLID = playlist.PLID, ID = ID };
                mediaPlaylist.BeginPosition = null;
                mediaPlaylist.EndPosition = null;
                mediaPlaylist.VideoListsDate = DateTime.Now;
            }
            else
            {
                if (mediaPlaylist.PLID == Guid.Empty) mediaPlaylist.PLID = playlist.PLID;
                if (mediaPlaylist.ID == Guid.Empty) mediaPlaylist.ID = ID;
            }

            Context.MediaPlaylists.InsertOnSubmit(mediaPlaylist);
            Context.SubmitChanges();
        }

        public void Remove (Playlist playlist, Media media)
        {
            AssertNotNull(playlist, "playlist");
            AssertNotNull(media, "media");
            Remove(playlist.PLID, media.ID);
        }

        public void Remove(Guid plid, Guid ID)
        {
            var VideoPlaylist = Context.MediaPlaylists.SingleOrDefault(vpl => vpl.PLID == plid && vpl.ID == ID);

            if (VideoPlaylist != null)
            {
                Context.MediaPlaylists.DeleteOnSubmit(VideoPlaylist);
                Context.SubmitChanges();
            }
        }

        public FindResult<Playlist> FindAll(FindParameters findParameters)
        {
            return ToFindResult<Playlist>(QueryFindAll(), findParameters);
        }

        public FindResult<Playlist> FindByUser(string loginName, FindParameters findParameters)
        {
            return ToFindResult<Playlist>(QueryFindByUser(loginName), findParameters);
        }

        public FindResult<Playlist> FindByUserWithMedia(string loginName, FindParameters findParameters)
        {
            return ToFindResult<Playlist>(QueryFindByUserWithMedia(loginName), findParameters);
        }

        public Playlist FindByNames(string playlistName, string loginName)
        {
            return Context.Playlists.SingleOrDefault(l => l.PlayListName == playlistName && l.LoginName == loginName);
        }

        #endregion

        #region IBasicService<Playlist> Members


        public void Create(Playlist playlist)
        {
            AssertNotNull(playlist, "playlist");
            InternalCreate(playlist);
        }

        public void Delete(Playlist playlist)
        {
            AssertNotNull(playlist, "playlist");
            InternalDelete(playlist.PLID);
        }

        public void Update(Playlist item)
        {
            throw new NotImplementedException();
        }

        public Playlist FindByID(Guid id)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
